import { mapGetters } from 'vuex';
import waves from "@/directive/waves";
// components
import Pagination from "@/components/Pagination";
import CreateDepartment from './components/create-department/create-department';
import DepartmentDetail from './components/department-detail/department-detail';
// provider
import { alertMessage, showAlertMessage, alertHtmlWithCallback } from "@/utils/utils";
import { getSysStructsTreeApi, deleteSelectedDepartmentApi, getSysStructsListApi } from './department-manage.service';
export default {
  name: 'department-management',
  components: { Pagination, CreateDepartment, DepartmentDetail },
  directives: { waves },
  data() {
    return {
      searchForm: {}, // 搜索内容
      confirmSearchForm: {}, // 确认提交搜索内容
      structsTree: [], // 组织树
      selectStructId: '', // 选中的组织Id
      // table列表分页
      checkedRows: [], // 批量选中的数据
      selectRow: '', // 选中的组织对象
      data: [], // 列表数据
      pageNum: 1, // 页码
      recordNum: 10, // 条数
      totalNum: 0, // 总数
      // 创建/编辑/详情弹窗
      detailDialogVisible: false, // 详情模态窗
      createOrEditDialogVisible: false, // 创建/编辑模态窗
      type: '' // 创建编辑类型 create:创建;edit:编辑
    }
  },
  computed: {
    ...mapGetters([
      'codesCheck'
    ])
  },
  created() {
    this.getSysStruct();
  },
  methods: {
    setIndex(index) {
      return (this.pageNum - 1) * this.recordNum + index + 1;
    },
    // 获取组织树
    getSysStruct() {
      getSysStructsTreeApi().then(res => {
        // 只显示2层节点
        res.data[0].children.forEach(element => {
          delete element.children
        })
        this.structsTree = res.data;
      });
    },

    // 根据选择部门获取组织列表
    getSysStructsList() {
      const data = {
        pid: this.selectStructId,
        code: this.confirmSearchForm.code,
        queryStr: this.confirmSearchForm.queryStr,
        pageNum: this.pageNum,
        recordNum: this.recordNum,
        level: 3
      }
      getSysStructsListApi(data).then(res => {
        this.data = res.data;
        this.totalNum = Number(res.dataCount);
      })
    },

    // 打开新建/编辑窗体
    openCreateOrEditDialog(type, row) {
      this.type = type;
      this.selectRow = row;
      this.createOrEditDialogVisible = true;
    },

    // 关闭新建/编辑窗体
    closeCreateOrEditDialog() {
      this.createOrEditDialogVisible = false;
    },

    // 打开详情窗体
    openDetailDialog(row) {
      this.selectRow = row;
      this.detailDialogVisible = true;
    },

    // 关闭详情窗体
    closeDetailDialog() {
      this.detailDialogVisible = false;
    },

    // 处理checkbox选中行数据
    handleSelectionChange(val) {
      this.checkedRows = val;
    },

    // 删除单/多条选中数据
    deleteSelectedData(id, name) {
      // 如果是单条删除
      let ids = id ? [id] : []
      if (ids.length === 0 && this.checkedRows.length === 0) {
        alertMessage('请至少选择一条数据', 'warning');
      } else {
        const message = {
          name: ids.length === 0 ? this.checkedRows.map(v => v.name).join('&nbsp;&nbsp;') : name
        }
        const alertMsg = showAlertMessage(`部门名称为:`, message)
        alertHtmlWithCallback(alertMsg, `是否确认删除已选中的${ids.length === 0 ? this.checkedRows.length : 1}条数据`).then(() => {
          if (ids.length === 0) {
            this.checkedRows.forEach(item => {
              ids.push(item.objectId)
            });
          }
          deleteSelectedDepartmentApi(ids).then((res) => {
            alertMessage(res.message);
            this.onReset();
          }).catch(() => { }); // 关闭confirm框的回调，不处理的话会报一个js error
        }).catch(() => { }); // 关闭confirm框的回调，不处理的话会报一个js error
      }
    },

    // 刷新树
    reload() {
      this.pageNum = 1;
      this.onReset();
      this.getSysStruct();
    },

    // 获取选中节点
    getSelectNode(data, node) {
      this.selectStructId = data.objectId;
      this.getSysStructsList();
    },

    // 搜索列表
    search() {
      this.pageNum = 1;
      this.confirmSearchForm = JSON.parse(JSON.stringify(this.searchForm));
      this.getSysStructsList();
    },

    // 刷新列表
    onReset() {
      this.pageNum = 1;
      this.confirmSearchForm = {
        code: '',
        queryStr: ''
      }
      this.selectStructId = '';
      this.searchForm = JSON.parse(JSON.stringify(this.confirmSearchForm))
      this.getSysStructsList();
    }
  }
}
